<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Admin\Controller\AdminController;

class UserController extends AdminController
{
    function _initialize() {
        parent::_initialize();
        $this->assign('sys', 'User'); // 数据集
    }
    /*
     * 用户列表 + 查询功能 + 分页
     *
     */
    public function index(){
        $users = M('Users');
        $user_name = I('get.user_name');
        $query_start_time = strstr(I('get.query_start_time'),' ',true);
        $query_end_time = strstr(I('get.query_end_time'),' ',true);
        if(IS_GET){
            if(!empty($user_name)){
                $datas['user_name'] = array('like',"%$user_name%");
            }
            if(!empty($query_start_time)){
                $datas['last_time'] = array('egt',$query_start_time);
            }
            if(!empty($query_end_time)){
                $datas['last_time'] = array('elt',$query_end_time);
            }
            if(!empty($query_end_time) && !empty($query_start_time)){
                $datas['last_time'] = array('between',"$query_start_time,$query_end_time");
            }
            $orderSerach = I('order');
            if(!empty($orderSerach)){
                switch ($orderSerach) {
                    case 0:
                        $order = "user_time desc";
                        break;
                    case 1:
                        $order = "user_orders desc";
                        $datas['user_orders'] = array('gt',0);
                        break;
                    case 2:
                        $order = "user_points desc";
                        $datas['user_points'] = array('gt',0);
                        break;
                    case 3:
                        $order = "user_login_num desc";
                        break;
                    default:
                        $order = "user_id asc";
                        break;
                }
            }else{
                $order = "user_id desc";
            }
        }
        $count = $users->where($datas)->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $list = $users->where($datas)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('pages',$show);// 赋值分页输出
        $this->assign('action', 'user');
        $this->assign('orderSerach', $orderSerach);
        $this->display();
    }

    public function store(){
        $users = M('Users');
        if(IS_GET){
            $user_name = I('get.user_name');
            if(!empty($user_name)){
                $user_name = I('get.user_name');
                $datas['user_name'] = array('like',"%$user_name%");
            }
        }
        $datas['user_group_id'] = 2;
        $count = $users->where($datas)->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $list = $users->where($datas)->order('user_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('pages',$show);// 赋值分页输出
        $this->assign('action', 'store');
        $this->display();
    }
    
    
    public function details(){
        //用户信息
        $where['user_id'] = I('user_id');
        $user = M('users')->where($where)->find();
        $this->assign('user', $user);
        
        //收货地址
        $address = M('address')->where($where)->select();
        $this->assign('address', $address);
        
        //浏览足迹
        $where2['member_id'] = I('user_id');
        $browse = M('goods_browse')->where($where2)->select();
        $this->assign('browse', $browse);
        
        
        //用户收藏(商品)
        $map['member_id'] = I('user_id');
        $map['fav_type'] = 20;
        $faviGoods = M('favorites')->where($map)->select();

        foreach ($faviGoods as $key => $value) {
            $Goods = M('Goods')->field('goods_id,goods_thumb,goods_title')->where('goods_id='.$value['fav_id'])->find();
            $faviGoods[$key]['img'] = $Goods['goods_thumb'];
            $faviGoods[$key]['title'] = $Goods['goods_title'];
        }
        $this->assign('faviGoods', $faviGoods);
        
        //用户收藏(店铺)
        $map2['member_id'] = I('user_id');
        $map2['fav_type'] = 40;
        $faviSupper = M('favorites')->where($map2)->select();
        foreach ($faviSupper as $key => $value) {
            $Supper = M('store')->field('store_id,store_label,store_name')->where('store_id='.$value['fav_id'])->find();
            $faviSupper[$key]['img'] = $Supper['store_label'];
            $faviSupper[$key]['title'] = $Supper['store_name'];
        }
        $this->assign('faviSupper', $faviSupper);

        $orderWhere['order_state'] = array('egt',20);
        $orderWhere['user_id'] = I('user_id');
        $userOrder = M('order')->where($orderWhere)->count();
        $userMoney = M('order')->where($orderWhere)->sum('order_amount');
        $this->assign('userOrder', $userOrder);
        $this->assign('userMoney', $userMoney);
        
        //用户订单
        //$where['order_state'] = 20;
//        $order = D('Order')->relation('OrderGoods')->where($where)->select();//订单信息
//        $this->assign('order', $order);
        
        $users = D('Order');
        $count = $users->relation('OrderGoods')->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $list = $users->relation('OrderGoods')->where($where)->order('order_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('order',$list);// 赋值数据集
        $this->assign('pages',$show);// 赋值分页输出
        
        
        $this->assign('action', 'user');
        $this->display();
    }





    /*
     * 删除用户
     *
     */
    public function deleteuser(){
        $data['user_id'] = I('get.user_id');
        $tfwxuser = M('Users')->where($data)->delete();
        if($tfwxuser){
            $this->success('操作成功',U('index'),3);

        }else{
            $this->error('操作失败');
        }
    }
}